package com.tjbklx1.redis.me;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisPublish {

	/**
	 * 给制定频道发布消息
	 * 
	 * @param channel
	 * @param message
	 * @return
	 */
	public static Long publish(String channel, String message) {
		Long ret = 0L;
		if (StringUtils.isEmpty(channel) || StringUtils.isEmpty(message)) {
			return ret;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			ret = jedis.publish(channel, message);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return ret;
	}
	
	
}
